/**
 * Created by Administrator on 2017/1/12.
 */
/**
 * Created by Administrator on 2016/12/8.
 */
// require.config({
//     paths : {
//         "jquery" : ["http://libs.baidu.com/jquery/2.0.3/jquery"]
//     }
// })
define('header', function($) {
    // $ 是 jquery 模块的输出
    var $ = require('jquery');
    var name = 'mjp';
    function printName(){
        console.log(name);
    }
    //验证码
    var mcolor=new Array(
        'rgba(110,100,20,1)',
        'rgba(20,110,110,1)',
        'rgba(210,200,40,1)',
        'rgba(40,200,210,1)',
        'rgba(50,50,50,1)',
        'rgba(255,100,20,1)',
        'rgba(110,255,20,1)',
        'rgba(110,100,255,1)',
        'rgba(20,150,20,1)',
        'rgba(150,100,200,1)'
    );
    var mrotate=new Array(
        10,15,20,30,-10,-15,-20,-30,5,-5
    )
    var mwidth=new Array(
        1,2,3,4,4
    );
    function drawwen(x,y)//event
    {
        var canvas = document.getElementById('myCanvas1');
        var context = canvas.getContext('2d');
        context.clearRect(0,0,200,200);
        var sum="";
        sessionStorage.removeItem("vli")
        for(var i=0;i<4;i++)
        {
            var n=Math.ceil(Math.random()*9)+"";
            sum=sum+n;
            var c=Math.ceil(Math.random()*9);
            var r=Math.ceil(Math.random()*9);
            var w=Math.ceil(Math.random()*4);
            setwen(n,c,r,w,context,i);
            var s=sessionStorage.vli||"";
            sessionStorage.setItem("vli",s+n);

        }
        context.globalCompositeOperation="lighter";
        context.fillStyle='#204d74';
        context.fillRect(0,0,80,40);
        // document.getElementById('button1').innerHTML=sum;
        // window.location=context.toDataURL('image/jpeg');//使其可以下载
    }
    function setwen(n,c,r,w,context,i)
    {
        context.fillStyle=mcolor[c];
        context.font='italic '+mwidth[w]+'0px sans-serif';
        context.textBaseline='top';
        context.textAlign='start';
        context.fillText(n,i*20,c,20);
    }
    var config={
        u1:{
            username:"mjp",
            password:"mjp11111"
        }
    }
    return {
        printName: printName,
        drawwen:drawwen,
        config:config
    };
});
module.exports = function(ngModule) {
    require('./header-login.scss');
    ngModule.directive('headerLogin',function () {
        return {
            restrict: 'E',//元素(element)
            scope: {},
            replace: true,
            template: require('./header-login.html'),//模板
            controllerAs: 'vm',// <=> $scope.vm = {greeting: '你好，我是卡哥'}
            controller: function ($scope,$http) {
                $scope.username="";
                $scope.password="";
                $scope.pass1="";
                $scope.pass2="";
                $scope.vil="";
                $scope.login=function () {
                    if($scope.username&&$scope.password){
                        var is_phone=/^1[34578]\d{9}$/.test($scope.username);
                        var is_email=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test($scope.username);
                        var send={pass:$scope.password};
                        if(is_phone){
                            send.phone=$scope.username
                        }else if(is_email){
                            send.email=$scope.username
                        }else{
                            layer.msg("请输入正确的用户名和密码")
                            return;
                        }
                        $http({
                            method:"post",
                            url:'/user/login',
                            data:{data:send}
                        }).then(function(data) {
                            if(data.data.msg.type==1)
                            {
                                layer.alert(data.data.msg.data);
                                sessionStorage.setItem("user",JSON.stringify(data.data.result))
                                location.href="../show/show.html"
                            }else{
                                layer.alert("登录密码或账户错误")
                            }
                        }).catch(function(data) {
                            layer.alert('Request faile')
                        });
                    }else{
                        layer.alert("请输入账户和密码")
                    }

                }
                $scope.sign=function () {
                    if(!$scope.username){
                        layer.alert("用户名不能为空")
                    }else{
                        if( $scope.pass1!==$scope.pass2){
                            layer.alert("两次密码不相等")
                            return;
                        }
                        if($scope.vil!=sessionStorage.vli){
                            layer.alert("验证码错误")
                            return;
                        }
                        var is_phone=/^1[34578]\d{9}$/.test($scope.username);
                        var is_email=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test($scope.username);
                        //判断手机是否存在
                        if(is_phone){
                            $http({
                                method:"post",
                                url:'/user/phone',
                                data:{data:{phone:$scope.username}}
                            }).then(function(data) {
                                if(data.data.msg.type!=1){
                                    layer.alert("手机已存在");
                                }else{
                                    $http({
                                        method:"post",
                                        url:'/user/create',
                                        data:{
                                            data:{
                                                title:  $scope.username,
                                                phone: $scope.username,
                                                integral: '5',
                                                grank: '1',
                                                grank_integral: '1',
                                                pass: $scope.pass1,
                                                is_phone:1 //验证
                                            }
                                        }
                                    }).then(function(data) {
                                        if(data.data.msg.type==1)
                                        {
                                            //发送短信
                                            $http({
                                                method:"post",
                                                url:'/user/getCode',
                                                data:{
                                                    data:{
                                                        phone:data.data.result.phone
                                                    }
                                                }
                                            }).then(function(data) {
                                                layer.msg("手机需要验证，已发送验证码成功")
                                            })
                                            setTimeout(function () {

                                                layer.prompt({
                                                    formType: 1,
                                                    value: '',
                                                    title: '请输手机验证码'
                                                }, function(value, index, elem){
                                                    $http({
                                                        method:"post",
                                                        url:'/user/checkCode',
                                                        data:{
                                                            data:{
                                                                phone:data.data.result.phone,
                                                                code:value
                                                            }
                                                        }
                                                    }).then(function(data) {
                                                        layer.alert("注册成功");
                                                    })
                                                    layer.close(index);
                                                });
                                            },1000)
                                        }else{
                                            layer.alert("手机信息有误")
                                        }
                                    }).
                                    catch(function(data) {
                                        layer.alert('系统错误')
                                    });
                                }

                            }).
                            catch(function(data) {
                                layer.alert('系统错误')
                            });

                        }
                        else if(is_email){
                            $http({
                                method:"post",
                                url:'/user/email',
                                data:{data:{email:$scope.username}}
                            }).then(function(data) {
                                if(data.data.msg.type!=1){
                                    layer.alert("邮箱已存在");
                                }else{
                                    $http({
                                        method:"post",
                                        url:'/user/create',
                                        data:{
                                            data:{
                                                title:  $scope.username,
                                                email: $scope.username,
                                                integral: '5',
                                                grank: '1',
                                                grank_integral: '1',
                                                pass: $scope.pass1,
                                                // is_email:1
                                            }
                                        }
                                    }).then(function(data) {
                                        if(data.data.msg.type==1)
                                        {
                                            layer.alert(data.data.msg.data)
                                            //发送email
                                            $http({
                                                method:"post",
                                                url:'/user/getEmail',
                                                data:{
                                                    data:{
                                                        email:data.data.result.email,
                                                        userid:data.data.result.userid
                                                    }
                                                }
                                            }).then(function(data) {
                                                layer.msg("已发送验证邮箱")
                                            })

                                        }else{
                                            layer.alert("信息有误")
                                        }
                                    }).
                                    catch(function(data) {
                                        layer.alert(data.data.msg.data)
                                    });
                                }

                            }).
                            catch(function(data) {
                                layer.alert('系统错误')
                            });
                        }
                        else{
                            layer.msg("请输入正确的邮箱或者用户");
                        }
                    }

                }
            }
        }
    });//定义指令，对应页面中的<hello-world></hello-world>
    require(['header'], function (my){
        var $ = require('jquery');
        $(function () {
            $("#myCanvas1").click(function(){
                my.drawwen();
            })
            console.log($("#sign_yan"))
            $("#sign_yan").focus(function(){
                //发送请求由php生成
                my.drawwen();
            })
            $("#login").click(function () {
                if($("#login_box").hasClass("hidden")){
                    $("#login_box").removeClass("hidden");
                    $("#sign_box").addClass("hidden");
                }
                else{
                    $("#login_box").removeClass("hidden");
                }
            })
            $("#sign").click(function () {
                if($("#sign_box").hasClass("hidden")){
                    $("#login_box").addClass("hidden");
                    $("#sign_box").removeClass("hidden");
                }
            })

        })
    })
}